#!/bin/bash

DEFAULT_DIR="l4d2"
DEFAULT_IP="0.0.0.0"
DEFAULT_PORT="27015"
DEFAULT_MAP="c2m1_highway"
DEFAULT_MODE="coop"
DEFAULT_CFG="server.cfg"
DEFAULT_TICK="100"
START_PARAMETERS="-strictportbind -nobreakpad -noassert -ip ${DEFAULT_IP} -port ${DEFAULT_PORT} +map ${DEFAULT_MAP} +mp_gamemode ${DEFAULT_MODE} +servercfgfile ${DEFAULT_CFG} -tickrate ${DEFAULT_TICK}"

STEAMCMD_URL="https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz"
STEAMCMD_QUICK_URI="https://gh-proxy.com/github.com/apples1949/SteamCmdLinuxFile/releases/download/steamcmd-latest/steamcmd_linux.tar.gz"
QUICK_UPDATE_PACKAGE="https://gh-proxy.com/github.com/apples1949/SteamCmdLinuxFile/releases/download/steamcmd-latest/package.tar.gz"

PLUGIN_VERSION=(-s -d -n)
DEFAULT_SH=$(cd $(dirname $0) && pwd)
folder_path=${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}/JS-MODS
l4d2_menu=${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}
plugins_name=${DEFAULT_DIR}_plugins.txt
progress_name=${DEFAULT_DIR}_progress.txt
selected_folders=()

if [ ! -e "$plugins_name" ]; then
    touch "$plugins_name"
fi

if [ ! -e "$progress_name" ]; then
    touch "$progress_name"
fi

function centos() {
    echo -e "\e[92m安装依赖...\e[0m"
    case "${VERSION_ID}" in
        7|8)
            sudo yum update
            sudo yum install glibc.i686 libstdc++.i686 curl screen zip unzip
        ;;
        *)
            echo -e "\e[34m不支持的系统版本\e[0m \e[31m${VERSION_ID}\e[0m"
            exit 1
        ;;
    esac

    if [ "${?}" -ne 0 ]; then
        echo -e "\e[31m依赖安装失败\e[0m"
        exit 1
    else
        echo -e "\e[92m依赖安装成功，开始安装服务器\e[0m"
    fi
}

function ubuntu() {
    echo -e "\e[92m安装依赖...\e[0m"
    sudo dpkg --add-architecture i386 && \
    sudo apt update && \
    case "${VERSION_ID}" in
        16.04|18.04|20.04)
            sudo apt -y install lib32gcc1 lib32stdc++6 lib32z1-dev curl screen zip unzip
        ;;
        22.04|24.04)
            sudo apt -y install lib32gcc-s1 lib32stdc++6 lib32z1-dev curl screen zip unzip
        ;;
        *)
            echo -e "\e[34m不支持的系统版本\e[0m \e[31m${VERSION_ID}\e[0m"
            exit 1
        ;;
    esac

    if [ "${?}" -ne 0 ]; then
        echo -e "\e[31m依赖安装失败\e[0m"
        exit 1
    else
        echo -e "\e[92m依赖安装成功\e[0m"
    fi
}

function debian() {
    echo -e "\e[92m安装依赖...\e[0m"
    sudo dpkg --add-architecture i386 && \
    sudo apt update && \
    case "${VERSION_ID}" in
        9|10)
            sudo apt -y install lib32gcc1 lib32stdc++6 lib32z1-dev curl screen zip unzip
        ;;
        11|12)
            sudo apt -y install lib32gcc-s1 lib32stdc++6 lib32z1-dev curl screen zip unzip
        ;;
        *)
            echo -e "\e[34m不支持的系统版本\e[0m \e[31m${VERSION_ID}\e[0m"
            exit 1
        ;;
    esac

    if [ "${?}" -ne 0 ]; then
        echo -e "\e[31m依赖安装失败\e[0m"
        exit 1
    else
        echo -e "\e[92m依赖安装成功\e[0m"
    fi
}

function install_dependencies() {
    if [ "$(id -u)" -ne 0 ]; then
        echo -e "\e[33m当前用户无root权限，自动跳过执行换源脚本\e[0m"
    else
        echo -e "\e[0m即将下载执行\e[31m换源脚本\e[0m"
        echo -e "\e[33m3秒内按任意键跳过，否则将自动执行...\e[0m"
        echo -e "\e[33m请在执行脚本后按上下键选择镜像源\e[0m"
        if read -t 3 -n 1 -s; then
            echo -e "\n\e[32m已跳过依赖换源脚本执行\e[0m"
        else
            echo -e "\n\e[32m正在执行依赖换源脚本...\e[0m"
            bash <(curl -sSL https://linuxmirrors.cn/main.sh) --use-intranet-source false --upgrade-software false --install-epel false --backup false --ignore-backup-tips
        fi
    fi

    
    source "/etc/os-release"
    case "${ID}" in
        ubuntu)
            ubuntu
        ;;
        debian)
            debian
        ;;
        centos)
            centos
        ;;
        *)
            echo -e "${ID}\e[34m不支持的操作系统\e[0m \e[31m${ID}\e[0m"
            exit 1
        ;;
    esac
}


function environment() {
    install_dependencies
    install_server
}

function download_and_extract_quick_package() {

    echo -e "\e[0m即将下载安装\e[31mSteamcmd快速更新包\e[0m"
    echo -e "\e[33m3秒内按任意键跳过，否则将自动执行...\e[0m"
        if read -t 3 -n 1 -s; then
            echo -e "\n\e[32m已跳过下载安装Steamcmd快速更新包"
        else
            local package_dir="${DEFAULT_SH}/steamcmd/package"
            
            echo -e "\e[34m正在清除package目录...\e[0m"
            rm -rf "${package_dir}"/*
            
            echo -e "\e[34m正在下载快速更新包...\e[0m"
            if curl -m 300 -fSLo "${package_dir}/package.tar.gz" "${QUICK_UPDATE_PACKAGE}"; then
                echo -e "\e[34m快速更新包下载成功，正在解压...\e[0m"
                if tar -zxf "${package_dir}/package.tar.gz" -C "${package_dir}"; then
                    echo -e "\e[92m快速更新包解压成功\e[0m"
                    # 删除下载的压缩包
                    rm -f "${package_dir}/package.tar.gz"
                    return 0
                else
                    echo -e "\e[31m快速更新包解压失败\e[0m"
                    rm -f "${package_dir}/package.tar.gz"
                    return 1
                fi
            else
                echo -e "\e[33m快速更新包下载失败，将使用常规安装方式\e[0m"
                return 1
            fi
        fi



}

function install_server() {
    trap 'rm -rf "${TMPDIR}"' EXIT
    TMPDIR=$(mktemp -d)
    if [ "${?}" -ne 0 ]; then
        echo -e "\e[31m临时目录\e[0m \e[31m创建失败\e[0m"
        exit 1
    fi

    if [ -f "${DEFAULT_SH}/steamcmd/steamcmd.sh" ] && \
       [ -f "${DEFAULT_SH}/steamcmd/linux32/crashhandler.so" ] && \
       [ -f "${DEFAULT_SH}/steamcmd/linux32/libstdc++.so.6" ] && \
       [ -f "${DEFAULT_SH}/steamcmd/linux32/steamcmd" ] && \
       [ -f "${DEFAULT_SH}/steamcmd/linux32/steamerrorreporter" ]; then
        echo -e "\e[34msteamcmd\e[0m 已经安装，跳过下载步骤"
        rm -rf "${TMPDIR}"

        download_and_extract_quick_package

        echo -e "\e[0m即将删除\e[31m服务端目录\e[0m"
        echo -e "\e[33m3秒内按任意键退出安装服务端，否则将在删除服务端后重新下载服务端...\e[0m"
        if read -t 3 -n 1 -s; then
            echo -e "\n\e[32m已退出脚本\e[0m"
            exit 1
        else
            echo -e "\n\e[32m正在删除安装服务端...\e[0m"
            rm -rf "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}"
        fi

        first_update_server
        return 0
    fi
    
    [ ! -d "${DEFAULT_SH}/steamcmd" ] && mkdir "${DEFAULT_SH}/steamcmd"
    
    if [ -f "${DEFAULT_SH}/steamcmd.tar.gz" ]; then
        echo -e "\e[34msteamcmd.tar.gz\e[0m 已经存在，正在解压"
        if ! tar -zxf "${DEFAULT_SH}/steamcmd.tar.gz" -C "${DEFAULT_SH}/steamcmd"; then
            rm -f "${DEFAULT_SH}/steamcmd.tar.gz"
            echo -e "\e[34msteamcmd.tar.gz\e[0m \e[31m解压失败，已删除\e[0m"
        else
            echo -e "\e[34msteamcmd\e[0m \e[92m解压成功\e[0m"
        fi
    fi

    echo -e "\e[34msteamcmd\e[0m 正在下载Github代理加速源 \e[92m${STEAMCMD_QUICK_URI}\e[0m"
    if ! curl -m 180 -fSLo "${TMPDIR}/steamcmd.tar.gz" "${STEAMCMD_QUICK_URI}"; then
        echo -e "\e[34msteamcmd\e[0m \e[31mGithub代理加速源(${STEAMCMD_QUICK_URI})下载失败 \e[0m"
        echo -e "\e[34msteamcmd\e[0m 尝试下载官方源\e[92m${STEAMCMD_URL}\e[0m"
        if ! curl --connect-timeout 10 -m 60 -fSLo "${TMPDIR}/steamcmd.tar.gz" "${STEAMCMD_URL}"; then
            echo -e "\e[34msteamcmd\e[0m \e[31m官方源\e[92m${STEAMCMD_URL}\e[0m下载失败\e[0m"
            exit 1
        fi
    fi

    echo -e "\e[34msteamcmd\e[0m \e[92m下载成功\e[0m"
    if ! tar -zxf "${TMPDIR}/steamcmd.tar.gz" -C "${DEFAULT_SH}/steamcmd"; then
        echo -e "\e[34msteamcmd.tar.gz\e[0m \e[31m解压失败\e[0m"
        rm -f "${TMPDIR}/steamcmd.tar.gz"
        exit 1
    fi
    
    rm -rf "${TMPDIR}"
    rm -rf "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}"
    first_update_server
}

function start_server() {
    stop_server
    ln_lib32
    screen -dmS "${DEFAULT_DIR}" "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}/srcds_run" -game left4dead2 ${START_PARAMETERS}
    sleep 1s
    screen -wipe > /dev/null 2>&1
    if ! screen -ls | grep -E "[0-9]+\.${DEFAULT_DIR}" > /dev/null 2>&1; then
        echo -e "\e[34m${DEFAULT_DIR}\e[0m \e[31m启动失败\e[0m"
        echo -e "\e[31m请检查相关参数是否配置正确\e[0m"
        exit 1
    else
        echo -e "\e[34m${DEFAULT_DIR}\e[0m \e[92m启动成功\e[0m"
        echo -e "\e[34m输入\e[0m \e[92mscreen -r ${DEFAULT_DIR}\e[0m \e[34m进入控制台\e[0m"
        echo -e "\e[34m快捷键\e[0m \e[92mCtrl + A + D\e[0m \e[34m退出控制台\e[0m"
    fi
}

function stop_server() {
    screen -wipe > /dev/null 2>&1
    screen -ls | grep -Eo "[0-9]+\.${DEFAULT_DIR}" | xargs -i screen -S {} -X quit
}

function restart_server() {
    start_server
}

function first_update_server() {
    stop_server
    echo -e "\e[34mleft4dead2\e[0m 安装中 \e[92m...\e[0m"

    if ! "${DEFAULT_SH}/steamcmd/steamcmd.sh" +force_install_dir "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}" +login anonymous +@sSteamCmdForcePlatformType windows +app_update 222860 validate +quit; then
        echo -e "\e[34mleft4dead2\e[0m \e[31m安装失败\e[0m"
        exit 1
    else
        if ! "${DEFAULT_SH}/steamcmd/steamcmd.sh" +force_install_dir "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}" +login anonymous +@sSteamCmdForcePlatformType linux +app_update 222860 validate +quit; then
            echo -e "\e[34mleft4dead2\e[0m \e[31m安装失败\e[0m"
            exit 1
        else
            echo -e "\e[34mleft4dead2\e[0m \e[92m安装成功\e[0m"
        fi
    fi
}

function update_server() {
    stop_server

    download_and_extract_quick_package

    echo -e "\e[34mleft4dead2\e[0m 安装中 \e[92m...\e[0m"
    if ! "${DEFAULT_SH}/steamcmd/steamcmd.sh" +force_install_dir "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}" +login anonymous +@sSteamCmdForcePlatformType linux +app_update 222860 validate +quit; then
        echo -e "\e[34mleft4dead2\e[0m \e[31m安装失败\e[0m"
        exit 1
    else
        echo -e "\e[34mleft4dead2\e[0m \e[92m安装成功\e[0m"
    fi
}

trap 'onCtrlC' INT
function onCtrlC () {
        kill -9 ${do_sth_pid} ${progress_pid}
        echo
        echo 你按了"CTRL+C"，已停止脚本
        exit 1
}

function do_sthi() {
        cp -rf "$folder_path/$folder_name/left4dead2" "$l4d2_menu"
}

function do_sthii() {
        for subfold in "${subfolde[@]}"; do
                rm -r "$l4d2_menu/$subfold"
        done
}

function progress() {
        local main_pid=$1
        local length=20
        local ratio=1
        while [ "$(ps -p ${main_pid} | wc -l)" -ne "1" ] ; do
                mark='>'
                progress_bar=
                for i in $(seq 1 "${length}"); do
                        if [ "$i" -gt "${ratio}" ] ; then
                                mark='-'
                        fi
                        progress_bar="${progress_bar}${mark}"
                done
                printf "操作中: ${progress_bar}\r"
                printf "操作中: ${progress_bar}\n" > "$progress_name"
                ratio=$((ratio+1))
                #ratio=`expr ${ratio} + 1`
                if [ "${ratio}" -gt "${length}" ] ; then
                        ratio=1
                fi
                sleep 0.1
        done
}

function progress_runi() {
do_sthi &
do_sth_pid=$(jobs -p | tail -1)
progress "${do_sth_pid}" &
progress_pid=$(jobs -p | tail -1)
wait "${do_sth_pid}"
cat "$progress_name"
}

function progress_runii() {
do_sthii &
do_sth_pid=$(jobs -p | tail -1)
progress "${do_sth_pid}" &
progress_pid=$(jobs -p | tail -1)
wait "${do_sth_pid}"
cat "$progress_name"
}

load_arrayi() {
if [ -s "$plugins_name" ]; then
    mapfile -t selected_folder < "$plugins_name"
else
    echo -e "\e[31m未安装插件，请安装插件后再使用此选项\e[0m"
    exit
fi
}

load_arrayii() {
if [ -s "$plugins_name" ]; then
    mapfile -t selected_folder < "$plugins_name"
fi
}

get_namei() {
load_arrayii

subfolders=($(ls -d "$folder_path"/*/))

count=1
for subfolder in "${subfolders[@]}"; do
    folder_name=$(basename "$subfolder")
    if [[ " ${selected_folder[*]} " == *" $folder_name "* ]]; then
        continue
    fi

    echo -e "\e[92m$count\e[0m.\e[34m$folder_name\e[0m"
    subfolde+=($folder_name)
    ((count++))
done
}

get_nameii() {
load_arrayi
count=1
for subfolder in "${selected_folder[@]}"; do
    folder_name=$(basename "$subfolder")
    echo -e "\e[92m$count\e[0m.\e[34m$folder_name\e[0m"
    ((count++))
done

}

plugins_install() {
echo -e "\e[36m请输入需要安装的插件数字，用分号\e[0m\e[41m（;）\e[0m\e[36m隔开\e[0m\e[41m（注意；数字如果错误一个则需要全部重新输入）\e[0m"
read user_input
IFS=";" read -ra input_numbers <<< "$user_input"

for number in "${input_numbers[@]}"; do
    if [[ $number =~ ^[0-9]+$ ]]; then
        index=$((number - 1))
        if [[ $number -ge 1 && $number -le ${#subfolde[@]} ]]; then
            selected_folders+=("${subfolde[number-1]}")
        else
            echo -e "\e[31m无效的数字\e[0m：\e[36m$number\e[0m，\e[31m请重新输入\e[0m"
        fi
        
        if ((index >= 0 && index < ${#subfolde[@]})); then
            selected_subfolder="${subfolde[index]}"
            folder_name=$(basename "$selected_subfolder")
            test_name+=($(basename "$folder_name"))
            echo -e "\e[46;34m正在安装插件\e[0m：\e[36m$folder_name\e[0m"
            progress_runi
            echo -e "\e[46;34m安装完成\e[0m"

        else
            echo -e "\e[31m无效的数字\e[0m：\e[36m$number\e[0m，\e[31m请重新输入\e[0m"
            plugins_install
        fi
    else
        echo -e "\e[31m无效的输入\e[0m：\e[36m$number\e[0m，\e[31m请重新输入\e[0m"
        plugins_install
    fi
done

printf "%s\n" "${test_name[@]}" >> "$plugins_name"


}

plugins_unload() {
echo -e "\e[36m请输入需要卸载的插件数字，用分号\e[0m\e[41m（;）\e[0m\e[36m隔开\e[0m\e[41m（注意；数字如果错误一个则需要全部重新输入）\e[0m"
read user_input
IFS=";" read -ra input_numbers <<< "$user_input"

for number in "${input_numbers[@]}"; do
    if [[ $number =~ ^[0-9]+$ ]]; then
        index=$((number - 1))
        if ((index >= 0 && index < ${#selected_folder[@]})); then
            selected_subfolder="${selected_folder[index]}"
            folder_name=$(basename "$selected_subfolder")
            test_name+=($(basename "$folder_name"))
            subfolde=($(find "$folder_path/$folder_name" -type f | sed "s|^$folder_path/$folder_name/||"))
            echo -e "\e[46;34m正在卸载插件\e[0m：\e[36m$folder_name\e[0m"
            progress_runii
            echo -e "\e[46;34m卸载完成\e[0m"
        else
            echo -e "\e[31m无效的数字\e[0m：\e[36m$number\e[0m，\e[31m请重新输入\e[0m"
            plugins_unload
        fi
    else
        echo -e "\e[31m无效的输入\e[0m：\e[36m$number\e[0m"
        plugins_unload
    fi
done

for name in "${test_name[@]}"; do
    grep -v "$name" "$plugins_name" > temp.txt
    mv temp.txt "$plugins_name"
done
}

function mixed_platform() {
    trap 'rm -rf "${DLDIR}"' EXIT
    DLDIR=$(mktemp -d)
    if [ "${?}" -ne 0 ]; then
        echo -e "\e[31m临时目录\e[0m \e[31m创建失败\e[0m"
        exit 1
    fi

    if [ -z "${1}" ]; then
        echo -e "\e[33m请选择要安装的插件平台版本:\e[0m"
        echo -e "\e[92m1\e[0m.\e[34m稳定版(默认)\e[0m"
        echo -e "\e[92m2\e[0m.\e[34m测试版\e[0m"
        read -p "您的选择是: " res
        if [ "${res}" == "2" ]; then
            MMS_URL=$(curl -s "https://www.sourcemm.net/downloads.php?branch=dev" | grep "download-link" | grep -Eo "https://[^']+linux.tar.gz" | sort -nr | head -n 1)
            SM_URL=$(curl -s "http://www.sourcemod.net/downloads.php?branch=dev" | grep "download-link" | grep -Eo "https://[^']+linux.tar.gz" | sort -nr | head -n 1)
        else
            MMS_URL=$(curl -s "https://www.sourcemm.net/downloads.php?branch=stable" | grep "download-link" | grep -Eo "https://[^']+linux.tar.gz" | sort -nr | head -n 1)
            SM_URL=$(curl -s "http://www.sourcemod.net/downloads.php?branch=stable" | grep "download-link" | grep -Eo "https://[^']+linux.tar.gz" | sort -nr | head -n 1)
        fi
    else
        if [ "${1}" == "-d" ]; then
            MMS_URL=$(curl -s "https://www.sourcemm.net/downloads.php?branch=dev" | grep "download-link" | grep -Eo "https://[^']+linux.tar.gz" | sort -nr | head -n 1)
            SM_URL=$(curl -s "http://www.sourcemod.net/downloads.php?branch=dev" | grep "download-link" | grep -Eo "https://[^']+linux.tar.gz" | sort -nr | head -n 1)
        else
            MMS_URL=$(curl -s "https://www.sourcemm.net/downloads.php?branch=stable" | grep "download-link" | grep -Eo "https://[^']+linux.tar.gz" | sort -nr | head -n 1)
            SM_URL=$(curl -s "http://www.sourcemod.net/downloads.php?branch=stable" | grep "download-link" | grep -Eo "https://[^']+linux.tar.gz" | sort -nr | head -n 1)
        fi
    fi

    echo -e "\e[34mmmsource\e[0m 下载中 \e[92m${MMS_URL}\e[0m"
    if ! curl --connect-timeout 10 -m 600 -fSLo "${DLDIR}/mmsource-linux.tar.gz" "${MMS_URL}"; then
        echo -e "\e[34mmmsource\e[0m \e[31m下载失败\e[0m"
        exit 1
    fi

    if ! tar -zxf "${DLDIR}/mmsource-linux.tar.gz" -C "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}/left4dead2"; then
        echo -e "\e[34mmmsource-linux.tar.gz\e[0m \e[31m解压失败\e[0m"
        exit 1
    fi

    sed -i '/"file"/c\\t"file"\t"..\/left4dead2\/addons\/metamod\/bin\/server"' "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}/left4dead2/addons/metamod.vdf"
    sed -i '/"file"/c\\t"file"\t"..\/left4dead2\/addons\/metamod\/bin\/server"' "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}/left4dead2/addons/metamod_x64.vdf"
    echo -e "\e[34mmmsource\e[0m \e[92m下载成功\e[0m"

    echo -e "\e[34msourcemod\e[0m 下载中 \e[92m${SM_URL}\e[0m"
    if ! curl --connect-timeout 10 -m 600 -fSLo "${DLDIR}/sourcemod-linux.tar.gz" "${SM_URL}"; then
        echo -e "\e[34msourcemod\e[0m \e[31m下载失败\e[0m"
        exit 1
    fi

    if ! tar -zxf "${DLDIR}/sourcemod-linux.tar.gz" -C "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}/left4dead2"; then
        echo -e "\e[34msourcemod-linux.tar.gz\e[0m \e[31m解压失败\e[0m"
        exit 1
    fi

    rm -f "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}/left4dead2/addons/sourcemod/plugins/nextmap.smx"
    echo -e "\e[34msourcemod\e[0m \e[92m下载成功\e[0m"
    rm -rf "${DLDIR}"
}

function ln_lib32() {
    [ -e "/lib32/libgcc_s.so.1" ] && [ -e "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}/bin/libgcc_s.so.1" ] && ln -sf "/lib32/libgcc_s.so.1" "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}/bin/libgcc_s.so.1"
    [ -e "/lib32/libstdc++.so.6" ] && [ -e "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}/bin/libstdc++.so.6" ] && ln -sf "/lib32/libstdc++.so.6" "${DEFAULT_SH}/steamcmd/${DEFAULT_DIR}/bin/libstdc++.so.6"
}

function main() {
    if [ "${#}" -gt 0 ]; then
        case "${1}" in
            -0|--install-all|0|"environment")
                echo "执行: 安装依赖软件包并下载游戏服务端"
                environment
                ;;
            -1|--deps-only|1|"install_dependencies")
                echo "执行: 安装依赖软件包"
                install_dependencies
                ;;
            -2|--server-only|2|"install")
                echo "执行: 下载游戏服务端"
                install_server
                ;;
            -3|--start|3|"start_server")
                echo "执行: 启动游戏服务端"
                start_server
                ;;
            -4|--stop|4|"stop_server")
                echo "执行: 停止游戏服务端"
                stop_server
                ;;
            -5|--restart|5|"restart"|"restart_server")
                echo "执行: 重启游戏服务端"
                restart_server
                ;;
            -6|--update|6|"update"|"update_server")
                echo "执行: 更新游戏服务端"
                update_server
                ;;
            -7|--install-plugins|7|"backup")
                echo "执行: 安装插件"
                get_namei
                plugins_install
                ;;
            -8|--remove-plugins|8|"recover")
                echo "执行: 卸载插件"
                get_nameii
                plugins_unload
                ;;
            -9|--install-platform|9|"mixed")
                echo "执行: 安装插件平台"
                if [ "${#}" -ge 2 ] && [[ "${PLUGIN_VERSION[@]}" =~ "${2}" ]]; then
                    mixed_platform "${2}"
                else
                    mixed_platform
                fi
                ;;
            -h|--help)
                show_help
                exit 0
                ;;
            *)
                echo "未知选项: ${1}"
                echo "使用 -h 或 --help 查看帮助信息"
                exit 1
                ;;
        esac
        # 如果有多个参数，提示只执行第一个
        if [ "${#}" -gt 1 ]; then
            echo "注意: 脚本只执行第一个启动项 '${1}'，忽略其余参数"
        fi
        exit 0
    else
        echo -e "\e[33m请输入数字并回车以选择要执行的操作:\e[0m"
        echo -e "\e[92m0\e[0m.\e[34m(初次选这个)安装依赖软件包并下载游戏服务端\e[0m"
        echo -e "\e[92m1\e[0m.\e[34m安装依赖软件包\e[0m"
        echo -e "\e[92m2\e[0m.\e[34m下载游戏服务端\e[0m"
        echo -e "\e[92m3\e[0m.\e[34m启动游戏服务端\e[0m"
        echo -e "\e[92m4\e[0m.\e[34m停止游戏服务端\e[0m"
        echo -e "\e[92m5\e[0m.\e[34m重启游戏服务端\e[0m"
        echo -e "\e[92m6\e[0m.\e[34m更新游戏服务端\e[0m"
        echo -e "\e[92m7\e[0m.\e[34m安装插件\e[0m"
        echo -e "\e[92m8\e[0m.\e[34m卸载插件\e[0m"
        echo -e "\e[92m9\e[0m.\e[34m安装插件平台\e[0m"
        read -p "您的选择是: " res
        case "${res}" in
            0)
                environment
                ;;
            1)
                install_dependencies
                ;;
            2)
                install_server
                ;;
            3)
                start_server
                ;;
            4)
                stop_server
                ;;
            5)
                restart_server
                ;;
            6)
                update_server
                ;;
            7)
                get_namei
                plugins_install
                ;;
            8)
                get_nameii
                plugins_unload
                ;;
            9)
                mixed_platform
                ;;
            *)
                echo "未知选项: ${res}"
                exit 1
                ;;
        esac
    fi
}

function show_help() {
    echo "用法: $0 [选项]"
    echo "选项:"
    echo "  -0, --install-all      安装依赖软件包并下载游戏服务端"
    echo "  -1, --deps-only        仅安装依赖软件包"
    echo "  -2, --server-only      仅下载游戏服务端"
    echo "  -3, --start            启动游戏服务端"
    echo "  -4, --stop             停止游戏服务端"
    echo "  -5, --restart          重启游戏服务端"
    echo "  -6, --update           更新游戏服务端"
    echo "  -7, --install-plugins  安装插件"
    echo "  -8, --remove-plugins   卸载插件"
    echo "  -9, --install-platform 安装插件平台"
    echo "  -h, --help             显示此帮助信息"
    echo ""
    echo "无参数时显示交互式菜单"
}

main ${*}
